import os
import inspect
import re
import ConfigParser

CLIENT_INFOFILENAME = 'current.info'
CLIENT_TMPFOLDER = '.gpublish'
SERVER_BASEFOLDER = 'gpublish'

class DocHandler(object):
    '''
    This object manages the behavior of the posting on Google Docs.
    
    When created, it looks in the current working directory, and looks
    for the preference file, and preference folder if it exists.
    
    If they don't it assigns default values to them, and create
    the relevant files and folders.

    When this is done, values in the preference file are assigned as
    attributes to this object.
    
    * Default Attributes *
    
    googledocs_basefolder : a string representing the folder in google
    docs under which this shall be saved. Recommended is 'gpublish-exports'

    googledocs_currentfolder : a string representing the folder in google
    docs under which this shall be saved. it defaults to the same name
    as the folder of the caller script.
    
    client_tmpfolder : a sting containing the client's tmp folder location
    client_infofile : a sting containing the client's infofile location

    '''
    def __init__(self, clear = False):
        self.curpath = os.getcwd()
        datapath = os.path.join(self.curpath, CLIENT_TMPFOLDER)

        self.client_tmpfolder = datapath
        # Checks for the tmp folder
        if not os.path.exists(datapath):
            os.mkdir(datapath)

        infofile = os.path.join(datapath, CLIENT_INFOFILENAME)
        self.client_infofile = infofile

        try:
            os.stat(infofile)
            file_present = True
        except OSError:
            file_present = False

        if not file_present or clear:
            self.make_default_infofile()

        self.load_infofile()

    def load_infofile(self):
        # set the attributes
        config = ConfigParser.RawConfigParser()
        config.read(self.client_infofile)
        
        self.server_basefolder = config.get('Server', 'basefolder')
        self.server_currentfolder = config.get('Server', 'currentfolder')

        self.client_tmpfolder = config.get('Client', 'tmpfolder')
        self.client_infofile = config.get('Client', 'infofile')

        self.login = config.get('Server', 'login')
        
    def set_login(self, login):
        config = ConfigParser.RawConfigParser()
        config.read(self.client_infofile)
        config.set('Server', 'login', login)
        self.login = login

        f = open(self.client_infofile, 'wb')
        config.write(f)
        f.close()
                
# The default info file
    def make_default_infofile(self):
        print 'Creating local info file'
        config = ConfigParser.RawConfigParser()
        
        # guess current folder
        basefolder = SERVER_BASEFOLDER
        currentfolder = self.curpath.split('/')[-1]
        
        config.add_section('Server')
        config.set('Server', 'basefolder', basefolder)
        config.set('Server', 'currentfolder', currentfolder)
        config.set('Server', 'login', '_undefined_')

        config.add_section('Client')
        config.set('Client', 'tmpfolder', self.client_tmpfolder)
        config.set('Client', 'infofile', self.client_infofile)


        f = open(self.client_infofile, 'wb')
        config.write(f)
        f.close()



















        


